<?php
class ClipboardsController extends AppController {

	var $name = 'Clipboards';
	var $helpers = array('Html', 'Form');
    
    function save( $id = null )
    {
        if($this->Session->read('User.type')=='jobseeker')
        {
            $this->Clipboard->recursive=-1;
            $data = $this->Clipboard->Job->find('count',array('conditions'=>array('Job.id'=>$id,'Job.status'=>1,'Job.end_date >='=>date("Y-m-d"))));        
            if($data)
            {
                $data = $this->Clipboard->find('count',array('conditions'=>array('Clipboard.jobseeker_id'=>$this->Session->read('User.id'),'Clipboard.job_id'=>$id)));        
                if(!$data)
                {
                    $data =  $this->Clipboard->find('count',array('conditions'=>array('Clipboard.jobseeker_id'=>$this->Session->read('User.id'))));
                    
                    if($data < 10)
                    {
                        $this->data['Clipboard']['job_id']=$id;
                        $this->data['Clipboard']['jobseeker_id']=$this->Session->read('User.id');
                        $this->Clipboard->create();
                        $this->Clipboard->save($this->data);
                        $this->Session->setFlash(__('Job has been saved to your clipboard', true));
                        $this->redirect(array('controller'=>'clipboards', 'action'=>'my_clipboards'),null, true);        
                        
                    }
                    else
                    {
                        $this->Session->setFlash(__('Sorry, you only can save 10 jobs in your clipboard', true));
                        $this->redirect(array('controller'=>'clipboards', 'action'=>'my_clipboards'),null, true);        
                    }
                }
                else
                {
                    $this->Session->setFlash(__('This Job has already in your clipboard', true));
                    $this->redirect(array('controller'=>'clipboards', 'action'=>'my_clipboards'),null, true);        
                }
            }
            else
            {
                $this->Session->setFlash(__('Invalid Job', true));
                $this->redirect(array('controller'=>'clipboards', 'action'=>'my_clipboards'),null, true);        
            }
        }        
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);        
        }
    }
    
    
    function my_clipboards()
    {
        if($this->Session->read('User.type')=='jobseeker')
        {
            $this->set('choose1',36);
            if(!empty($this->data))
            {
                for($i=0;$i<$this->data['jumlah'];$i++)
                {
                    $temp = 'check'.$i;
                    if( !empty($this->data[$temp]) )
                    {
                        $this->Clipboard->del($this->data[$temp]);
                    }               
                }
                $this->Session->setFlash(__('Saved Job(s) has been deleted from your clipboard', true));
            }
            
            
            $query = "SELECT clipboards.id, clipboards.created, jobs.created, jobs.end_date, jobs.id, jobs.position, employers.id, employers.company_name FROM `employers`,`jobs`,`clipboards` WHERE jobs.id=clipboards.job_id AND jobs.employer_id=employers.id AND clipboards.jobseeker_id =".$this->Session->read('User.id')." ORDER BY clipboards.created DESC";
            $data = $this->Clipboard->query($query);
            $this->set('clipboards',$data);
        }    
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'jobseekers', 'action'=>'login'),null, true);        
        }
    }

}
?>